package com.ymtms.costaccounting.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ymtms.costaccounting.dao.FinancialFeeMapper;
import com.ymtms.costaccounting.exception.FinancialFeeException;
import com.ymtms.costaccounting.pojo.FinancialFee;
import com.ymtms.costaccounting.service.FinancialFeeService;
import com.ymtms.costaccounting.vo.RequestBean;
import com.ymtms.costaccounting.vo.Result;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @auther: autumn-luo
 * @description:
 * @createTime: 2019-10-30 10:59
 */
@Log4j2
@Service
public class FinancialFeeServiceImpl implements FinancialFeeService {

    @Autowired
    FinancialFeeMapper financialFeeMapper;


    @Override
    public Result ffeeAll(RequestBean requestBean, String ffeeInfo) {

        // 分页数据处理
        // 获取当前页和页大小
        Integer currentPage = requestBean.getPage();
        Integer pageSize = requestBean.getLimit();

        // 判断是否有排序,有则生成符合pagehelper的排序字符串
        String sidx = requestBean.getSidx(); // 排序参数
        String sord = requestBean.getSord(); // 排序规则

        String orderBy = "";
        // pagehelper静态设置页属性方法，它会拦截下面的第一个查询！第二个无效。
        if (checkOrderBy(sidx, sord)) {
            sidx = upperCharToUnderLine(sidx);
            orderBy = sidx + " " + sord;
            PageHelper.startPage(currentPage, pageSize, orderBy);
        } else {
            // 设置当前页和页大小
            PageHelper.startPage(currentPage, pageSize);
        }
        // 查询结果传入排序
        List<FinancialFee> ffeeAll = financialFeeMapper.selectFfeeAll(ffeeInfo);
        log.info("ffeeAll" + ffeeAll);

        // 利用当前页和页大小配置pageinfo
        PageInfo<FinancialFee> pageList = new PageInfo<>(ffeeAll);
        // 新建一个封装类对象，设置参数，传入查询得到的数据
        Result<FinancialFee> finfeeInfo = new Result<>();
        if (ffeeAll.size() == 0) {
            // 存入查询数据
            finfeeInfo.setData(null);
            // 存入总条数
            finfeeInfo.setCount(1);
        } else {
            // 存入查询数据
            finfeeInfo.setData(ffeeAll);
            // 存入总条数
            finfeeInfo.setCount(pageList.getTotal());
        }

        return finfeeInfo;
    }

    @Override
    public Integer ffeeDel(Integer ffid) {
        log.info("ffid" + ffid);
        Integer ffeeDelResult = financialFeeMapper.ffeeDel(ffid);
        if (ffeeDelResult == 0) {
            throw new FinancialFeeException("财务费用数据删除失败！");
        } else {
            return ffeeDelResult;
        }

    }

    @Override
    public Integer ffeeUpdate(FinancialFee financialFee) {
        financialFee.setFlag(0);
        Integer ffeeUpdateResult = financialFeeMapper.updateByPrimaryKey(financialFee);
        if (ffeeUpdateResult == 0) {
            throw new FinancialFeeException("修改财务费用出错");
        } else {
            return ffeeUpdateResult;
        }


    }

    @Override
    public Integer ffeeAdd(FinancialFee financialFee) {
        financialFee.setFlag(0);
        Integer ffeeAddResult = financialFeeMapper.insert(financialFee);
        if (ffeeAddResult == 0) {
            throw new FinancialFeeException("新增财务费用出错");
        } else {
            return ffeeAddResult;
        }

    }

    // 判断是否非空
    private boolean checkOrderBy(String sidx, String sord) {
        if (sidx != null && sord != null
                && sidx.trim().length() > 0
                && sord.trim().length() > 0) {
            return true;
        }
        return false;
    }

    // 大写转换为下划线+小写
    public static String upperCharToUnderLine(String param) {
        Pattern p = Pattern.compile("[A-Z]");
        if (param == null || param.equals("")) {
            return "";
        }
        StringBuilder builder = new StringBuilder(param);
        Matcher mc = p.matcher(param);
        int i = 0;
        while (mc.find()) {
            builder.replace(mc.start() + i, mc.end() + i, "_" + mc.group().toLowerCase());
            i++;
        }

        if ('_' == builder.charAt(0)) {
            builder.deleteCharAt(0);
        }
        return builder.toString();
    }
}
